using System.Collections.Generic;
using System.Threading.Tasks;
using CoreApp.DBAccess;
using CoreApp.DBModel;
using Dapper;
using Microsoft.Extensions.Options;
using System.Data;
using System.Linq;
using System;
using CoreApp.Utility;

namespace CoreApp.DapperAccess
{
    public class UserRepository : RepositoryBase<UserInfo>, IUserRepository
    {
        public UserRepository(IOptions<DBConnectionConfig> option) : base(option)
        {
        }

        public async Task<int> InsertUser(UserInfo entity)
        {
            try
            {
                string sql = @"INSERT INTO userinfo (Name, Pwd) VALUES(@Name, @Pwd)";
                return await Insert(entity, sql);
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }              
        }

        public async Task<int> DeleteUserById(int Id)
        {
            try
            {
                string sql = @"DELETE FROM userinfo WHERE ID=@Id";
                return await Delete(Id, sql);   
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }             
        }

        public async Task<int> UpdateUser(UserInfo entity)
        {
            try
            {
                string sql = @"UPDATE userinfo SET Name=@Name, Pwd=@Pwd WHERE ID=@ID";
                return await Update(entity, sql);   
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }            
        }

        public async Task<UserInfo> QueryUser(string name, string pwd)
        {
            try
            {
                string sql = @"SELECT id,name,pwd FROM userinfo where name=@name and pwd=@pwd";
                using (conn)
                {
                    return await conn.QueryFirstOrDefaultAsync<UserInfo>(sql, new { name = name, pwd = pwd });
                }   
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }

        public async Task<List<UserInfo>> GetUserListAsnync(string name)
        {
            try
            {
                string sql = @"SELECT id,name,pwd FROM userinfo WHERE name=@name";
                using (conn)
                {
                    return await Task.Run(() => conn.Query<UserInfo>(sql, new { name = name }).ToList());
                } 
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }

        public List<UserInfo> GetUserList(string name)
        {
            try
            {
                string sql = @"SELECT id,name,pwd FROM userinfo WHERE name=@name";
                using (conn)
                {
                    return conn.Query<UserInfo>(sql, new { name = name }).ToList();
                } 
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }

        public async Task<List<UserInfo>> GetUserByProc(string procName, Dictionary<string, string> dic)
        {
            try
            {
                DynamicParameters dp = new DynamicParameters();
                if (dic.Count() > 0)
                {
                    foreach (var item in dic)
                    {
                        switch (item.Key.Trim().ToLower())
                        {
                            case "name":
                                dp.Add("@uName", item.Value.Trim(), DbType.String);
                                break;
                        }
                    }
                }
                using (conn)
                {
                    return await Task.Run(() => conn.Query<UserInfo>(procName, dp, null, true, null, CommandType.StoredProcedure).ToList());
                } 
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }
    }
}